package cn.azzhu.hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;

/**
 * @author azzhu
 * @create 2020-08-17 11:03:47
 */
@Controller
@RequestMapping("user2")
@SessionAttributes("loginUser")
public class HelloController {
    //@RequestMapping(value = "hello",method = RequestMethod.GET)
    @GetMapping("hello")
    public String hello(Map<String,Object> map) {
        //int a = 10 /0;
        System.out.println("===================>hello=============");
        map.put("msg","hellomvc");
        return "hello";
    }

   // @ExceptionHandler(value = ArithmeticException.class)
    public ModelAndView error(Exception e) {
        System.out.println("******Exception msg is:  "+e.getMessage());
        ModelAndView mv = new ModelAndView();
        mv.addObject("myexception", e);
        mv.setViewName("error");
        return mv;
    }

    //user/del/1
    @GetMapping("del/{id}/{uName}")
    public String del(@PathVariable("id") Integer id,
                      @PathVariable(name="uName",required = false) String uName) {
        System.out.println("=====>" +id + uName);
        return "ok";
    }

    @GetMapping("getParam")
    public String del(@RequestParam("id") String id,
                      @RequestParam(name = "pageNum",required = false,defaultValue = "1") Integer pageNum) {
        System.out.println("=====>"+id+":"+pageNum);
        return "hello";
    }

    //post怎么去测？？
    @PostMapping("add")
    public String add(User user,@RequestParam("hobby") String[] hobby) {
        System.out.println(user+":"+ Arrays.toString(hobby));
        return "hello";
    }

    @GetMapping("model")
    public ModelAndView model(Map<String,Object> map, Model model, ModelMap modelMap) {
        final ModelAndView mv = new ModelAndView();
        mv.addObject("mvkey","mvValue");
        map.put("mapKey","mapVal");
        model.addAttribute("modelKey","modelVal");
        modelMap.addAttribute("modelMapKey","modelMapVal");

        //获取每个的类型
        System.out.println(map.getClass());
        System.out.println(model.getClass());
        System.out.println(modelMap.getClass());
        System.out.println(map == model);
        System.out.println(map == modelMap);
        //需要设置逻辑视图名
        mv.setViewName("hello");
        return mv;
    }

    //跳转到修改页面
    @GetMapping("toUpdate")
    public String toUpdate(Map<String,Object> map) {
        System.out.println("toupdate enter.....");
        //应该根据id去数据库查询：在页面中进行回显，实际上db中保存了4个或者5个
        //点击修改，只能带过去3个值 email
        User user = new User();
        user.setAge(10);
        user.setId(1001);
        user.setUserName("zs");

        map.put("user",user);
        return "hello";
    }

    @PostMapping("update")
    public String update(User user) {
        System.out.println("====>update"+user);
        //uuser.getId
        //应该作为参数，向service传递
        //update user set userName = zz,age =1,email=null,hobboy= 2 where id= 1
        //userService.update(user)
        return "ok";
    }

    @ModelAttribute
    public void getUserById(@RequestParam(value="id",required=false) Integer id,
                            Map<String,Object> map) {
        System.out.println("come in @ModelAttribute......");
        if (null != id) {
            //模拟根据ID从数据库查询出user对象的原始数据。
            User user = new User();
            user.setAge(10);
            user.setId(1001);
            user.setUserName("zs");
            user.setEmail("aaaaa");
            user.setHobby(new String[]{"aa","bb"});
            map.put("user", user);
        }
    }

    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Map<String,Object> map, HttpSession session)
    {
        User user = new User();
        user.setAge(10);
        user.setId(1001);
        user.setUserName("zs");
        user.setEmail("aaaaa");
        map.put("loginUser",user);  //req域名中
        map.put("nickName","狗蛋");
        return "forward:hello";
    }

    @GetMapping("aa")
    public String toA() {
        return "redirect:index.jsp";
    }

    //@RequestMapping(value="/del/{userId}",method=RequestMethod.DELETE)
    @DeleteMapping("/del/{userId}")
    public String testRestfulDelete(@PathVariable("userId") Integer userId)
    {
        System.out.println("=====testRestfulDelete:"+userId);
        return "ok";
    }
}
